﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Octopus.Logging.Impl.Text.Common.IO
{
    public class CountingQuietTextWriter : QuietTextWriter
    {
        #region Public Instance Constructors

        /// <summary>
        /// Creates a new instance of the <see cref="CountingQuietTextWriter" /> class 
        /// with the specified <see cref="TextWriter" /> and <see cref="IErrorHandler" />.
        /// </summary>
        /// <param name="errorHandler">The <see cref="IErrorHandler" /> to report errors to.</param>
        public CountingQuietTextWriter(string file,bool append, int flushTimeSpan)
            : base(file,append,flushTimeSpan)
        {
            m_countBytes = 0;
        }

        #endregion Public Instance Constructors

        #region Override implementation of QuietTextWriter

        /// <summary>
        /// Writes a string to the output and counts the number of bytes written.
        /// </summary>
        /// <param name="str">The string data to write to the output.</param>
        override public void Write(string str)
        {
            try
            {
                base.Write(str);

                // get the number of bytes needed to represent the 
                // string using the supplied encoding.
                m_countBytes += this.Writer.Encoding.GetByteCount(str);
            }
            catch (Exception e)
            {

            }
        }

        #endregion Override implementation of QuietTextWriter

        #region Public Instance Properties

        /// <summary>
        /// Gets or sets the total number of bytes written.
        /// </summary>
        /// <value>
        /// The total number of bytes written.
        /// </value>
        public long Count
        {
            get { return m_countBytes; }
            set { m_countBytes = value; }
        }

        #endregion Public Instance Properties

        #region Private Instance Fields

        /// <summary>
        /// Total number of bytes written.
        /// </summary>
        private long m_countBytes;

        #endregion Private Instance Fields
    }
}
